<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\TestReportService;
use Session;

class TestReportController extends Controller{
    public function list(){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $testreport = new TestReportService;
            $data['username'] = $username;
            $data['report_list'] = $testreport->getReportList();

            return view('admin.report.list',$data);
        }
    }

    public function create(){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $data['username'] = $username;
            return view('admin.report.add',$data);
        }
    }

    public function store(){
        $testreport = new TestReportService;
        date_default_timezone_set('PRC');

        $data['name'] = addslashes($_POST['name']);
        $data['content'] = addslashes($_POST['content']);
        $data['add_date'] = date('Y-m-d H:i:s');

        $res = $testreport->insertReport($data);
        if($res){
            $_data = array('code'=>200,'msg'=>'测试报告添加成功');
        }else{
            $_data = array('code'=>400,'msg'=>'测试报告添加失败');
        }
        return response($_data);
    }

    public function edit($id){
        $username = Session::get('username');
        if(!$username){
            return redirect('/admin/login');
        }else{
            $testreport = new TestReportService;
            $data['username'] = $username;
            $data['report_info'] = $testreport->getReportInfo($id);

            return view('admin.report.edit',$data);
        }
    }

    public function update(){
        $testreport = new TestReportService;
        date_default_timezone_set('PRC');
        $id = intval($_POST['id']);
        $data['name'] = addslashes($_POST['name']);
        $data['content'] = addslashes($_POST['content']);
        $data['update_date'] = date('Y-m-d H:i:s');

        $res = $testreport->updateReport($data,$id);
        if($res){
            $_data = array('code'=>200,'msg'=>'测试报告添加成功');
        }else{
            $_data = array('code'=>400,'msg'=>'测试报告添加失败');
        }
        return response($_data);
    }

    public function del($id){
        $testreport = new TestReportService;
        $res = $testreport->delReport($id);
        if($res){
            $_data = array('code'=>200,'msg'=>'测试报告删除成功');
        }else{
            $_data = array('code'=>400,'msg'=>'测试报告删除失败');
        }

        return response()->json($_data);
    }
}
